﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetMsSqlLibrary
{
    public class Tools
    {
        /// <summary>
        /// DataReader转DataTable
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static DataTable ConvertDataReaderToDataTable(SqlDataReader reader, string tableName)
        {
            try
            {
                DataTable objDataTable = new DataTable(tableName);
                int intFieldCount = reader.FieldCount;
                for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
                {
                    objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
                }

                objDataTable.BeginLoadData();

                object[] objValues = new object[intFieldCount];
                while (reader.Read())
                {
                    reader.GetValues(objValues);
                    objDataTable.LoadDataRow(objValues, true);
                }
                reader.Close();
                objDataTable.EndLoadData();

                return objDataTable;

            }
            catch (Exception ex)
            {
                throw new Exception("转换出错!", ex);
            }

        }

        /// <summary>
        /// 根据表名创建DataTable
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static DataTable CreatDataTableQuick(string tableName)
        {
            SqlConnection con = SqlManager.GetCon();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            DataTable table = new DataTable(tableName);
            string sqlStr = "select name from syscolumns where id=object_id('" + tableName + "')";
            try
            {

                cmd.CommandText = sqlStr;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                object[] objValues = new object[1];
                while (dr.Read())
                {
                    dr.GetValues(objValues);
                    table.Columns.Add(objValues[0].ToString());
                }
            }
            catch (Exception ex)
            {
                table = null;
                throw ex;
            }
            finally
            {
                con.Close();
            }
            return table;
        }

        /// <summary>
        /// 提供字段组创建Datatable
        /// </summary>
        /// <param name="fileds"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static DataTable CreatDataTable(string[] fileds,string tableName = null)
        {
            DataTable table = new DataTable(tableName);
            foreach (string item in fileds)
            {
                table.Columns.Add(item);
            }
            return table;
        }
    }
}
